package cn.qiyu5522.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: BeanCopyUtils
 * Package: cn.qiyu5522.utils
 * Description:
 *
 * @Author: Tonkey
 * @Create：2024/4/10-17:29
 * @Version: v1.0
 */
public class BeanCopyUtils {

    private BeanCopyUtils() {
    }


    // 单个实体类的拷贝
    public static <V> V copyBean(Object source, Class<V> clazz) {

        // 创建目标对象
        V result = null;
        try {
            result = clazz.newInstance();
            // 实现属性copy
            BeanUtils.copyProperties(source, result);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        // 返回结果
        return result;
    }

    public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
        return list.stream().map( o-> copyBean(o, clazz)).collect(Collectors.toList());
    }
}
